package com.njkwt.business.system.controller;

import com.njkwt.business.system.dto.ModuleElementDto;
import com.njkwt.business.system.dto.PageDto;
import com.njkwt.business.system.dto.ResponseDto;
import com.njkwt.business.system.service.ModuleElementService;
import com.njkwt.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
* @ClassName ModuleElementController
* @Description
* @Author ydj (171799383@qq.com)
* @Date  2020-8-18 14:58:16
* @Version V1.0
**/

@RestController
@RequestMapping("/api/business/system/moduleElement")
public class ModuleElementController {

    private static final Logger LOG = LoggerFactory.getLogger(ModuleElementController.class);
    public static final String BUSINESS_NAME = "模块元素表(需要权限控制的按钮)";

    @Resource
    private ModuleElementService moduleElementService;

    /**
     * 列表查询
     */
    @PostMapping("/list")
    public ResponseDto list(@RequestBody PageDto pageDto) {
        ResponseDto responseDto = new ResponseDto();
        moduleElementService.list(pageDto);
        responseDto.setContent(pageDto);
        return responseDto;
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public ResponseDto save(@RequestBody ModuleElementDto moduleElementDto) {
        // 保存校验
        ValidatorUtil.require(moduleElementDto.getId(), "流水号");
        ValidatorUtil.length(moduleElementDto.getId(), "流水号", 1, 50);
        ValidatorUtil.require(moduleElementDto.getDomid(), "DOM ID");
        ValidatorUtil.length(moduleElementDto.getDomid(), "DOM ID", 1, 255);
        ValidatorUtil.require(moduleElementDto.getName(), "名称");
        ValidatorUtil.length(moduleElementDto.getName(), "名称", 1, 255);
        ValidatorUtil.require(moduleElementDto.getAttr(), "元素附加属性");
        ValidatorUtil.require(moduleElementDto.getScript(), "元素调用脚本");
        ValidatorUtil.require(moduleElementDto.getIcon(), "元素图标");
        ValidatorUtil.length(moduleElementDto.getIcon(), "元素图标", 1, 255);
        ValidatorUtil.require(moduleElementDto.getClassify(), "元素样式");
        ValidatorUtil.length(moduleElementDto.getClassify(), "元素样式", 1, 255);
        ValidatorUtil.require(moduleElementDto.getRemark(), "备注");
        ValidatorUtil.length(moduleElementDto.getRemark(), "备注", 1, 200);
        ValidatorUtil.require(moduleElementDto.getModuleid(), "功能模块Id");
        ValidatorUtil.length(moduleElementDto.getModuleid(), "功能模块Id", 1, 50);
        ValidatorUtil.length(moduleElementDto.getTypename(), "分类名称", 1, 20);
        ValidatorUtil.length(moduleElementDto.getTypeid(), "分类ID", 1, 50);

        ResponseDto responseDto = new ResponseDto();
        moduleElementService.save(moduleElementDto);
        responseDto.setContent(moduleElementDto);
        return responseDto;
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id) {
        ResponseDto responseDto = new ResponseDto();
        moduleElementService.delete(id);
        return responseDto;
    }
}
